<?php
/**
 * 模板使用示例
 * 
 * 用法：php template-usage.php [示例类型]
 * 示例类型：
 *   basic    - 基础海报示例
 *   product  - 商品海报示例
 *   event    - 活动海报示例
 *   batch    - 批量生成示例
 *   string   - HTML字符串示例
 *   member   - 会员海报示例
 *   all      - 运行所有示例（默认）
 */

// 获取命令行参数
$example_type = isset($argv[1]) ? strtolower($argv[1]) : 'all';

// 创建HtmlToImageService实例
require_once __DIR__ . '/../HtmlToImageService.php';
$htmlToImage = new HtmlToImageService('node', null, null, true);

// 设置默认选项
$defaultOptions = [
  'fullPage' => false,
  'element' => true,
  'elementWithBg' => true,
  'pdf' => false,
  'domain' => 'https://example.com'
];

echo "开始生成海报...\n";

// 基础海报示例
function runBasicExample($htmlToImage, $defaultOptions) {
  echo "\n=== 基础海报模板示例 ===\n";
  
  $basicPosterData = [
    'title' => '限时特惠活动',
    'subtitle' => '只在本周末',
    'text' => '这是一个超值促销活动，全场商品8折起，新用户还可享受额外95折优惠！',
    'price' => '¥199.00',
    'shopName' => '优品商城',
    'shopDesc' => '品质生活从这里开始',
    'isShowPrice' => true,
    'isShowShopInfo' => true,
    'mainImage' => 'https://dummyimage.com/400x300/ff9800/ffffff&text=特惠活动',
    'qrcode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/sale',
    'shopLogo' => 'https://dummyimage.com/200x80/3f51b5/ffffff&text=优品商城',
    'domain' => 'https://example.com'
  ];
  
  $result = $htmlToImage->generateFromTemplate(
    'templates/basic-poster.html',
    'basic_poster_example',
    $basicPosterData,
    true,
    $defaultOptions
  );
  
  if ($result['success']) {
    echo "基础海报生成成功！\n";
    echo "文件路径：\n";
    foreach ($result['files'] as $type => $path) {
      if ($path) echo "- {$type}: {$path}\n";
    }
  } else {
    echo "生成失败: " . $result['error'] . "\n";
  }
}

// 商品海报示例
function runProductExample($htmlToImage, $defaultOptions) {
  echo "\n=== 商品海报模板示例 ===\n";
  
  $productPosterData = [
    'title' => '高品质无线蓝牙耳机',
    'description' => '采用先进降噪技术，提供40小时超长续航，适合运动和日常使用。人体工学设计，佩戴舒适。',
    'price' => '¥399',
    'originalPrice' => '¥599',
    'discount' => '6.7折',
    'showDiscount' => true,
    'shopName' => '科技数码专营店',
    'productImage' => 'https://dummyimage.com/300x400/6200ea/ffffff&text=蓝牙耳机',
    'qrCode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/earphone',
    'domain' => 'https://example.com'
  ];
  
  $result = $htmlToImage->generateFromTemplate(
    'templates/product-poster.html',
    'product_poster_example',
    $productPosterData,
    true,
    $defaultOptions
  );
  
  if ($result['success']) {
    echo "商品海报生成成功！\n";
    echo "文件路径：\n";
    foreach ($result['files'] as $type => $path) {
      if ($path) echo "- {$type}: {$path}\n";
    }
  } else {
    echo "生成失败: " . $result['error'] . "\n";
  }
}

// 活动海报示例
function runEventExample($htmlToImage, $defaultOptions) {
  echo "\n=== 活动海报模板示例 ===\n";
  
  $eventPosterData = [
    'title' => '年中大促销',
    'date' => '2023年7月1日-7月15日',
    'discountValue' => '3折起',
    'discountDescription' => '全场大优惠',
    'description' => '年中大促，全场低至3折，多买多送，满1000减200，还有超值赠品等你来拿！',
    'shopName' => '优尚百货',
    'shopAddress' => '地址：城市中心购物广场A区123号',
    'limitedTimeTag' => '限时抢购',
    'backgroundColor' => 'linear-gradient(to bottom, #8e2de2, #4a00e0)',
    'shopLogo' => 'https://dummyimage.com/200x80/8e2de2/ffffff&text=优尚百货',
    'qrcode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/sale',
    'domain' => 'https://example.com'
  ];
  
  $result = $htmlToImage->generateFromTemplate(
    'templates/event-poster.html',
    'event_poster_example',
    $eventPosterData,
    true,
    $defaultOptions
  );
  
  if ($result['success']) {
    echo "活动海报生成成功！\n";
    echo "文件路径：\n";
    foreach ($result['files'] as $type => $path) {
      if ($path) echo "- {$type}: {$path}\n";
    }
  } else {
    echo "生成失败: " . $result['error'] . "\n";
  }
}

// 批量生成示例
function runBatchExample($htmlToImage, $defaultOptions) {
  echo "\n=== 批量生成商品海报示例 ===\n";
  
  $products = [
    [
      'title' => '智能手表 Pro Max',
      'description' => '24小时心率监测，50米防水，多种运动模式，续航长达14天。',
      'price' => '¥899',
      'originalPrice' => '¥1299',
      'discount' => '6.9折',
      'shopName' => '智能穿戴旗舰店',
      'productImage' => 'https://dummyimage.com/300x400/00a0f0/ffffff&text=智能手表',
      'qrCode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/watch',
      'domain' => 'https://example.com'
    ],
    [
      'title' => '超轻薄笔记本电脑',
      'description' => '最新11代处理器，16GB内存，512GB固态硬盘，高清触摸屏。',
      'price' => '¥5999',
      'originalPrice' => '¥7999',
      'discount' => '7.5折',
      'shopName' => '电脑数码专卖',
      'productImage' => 'https://dummyimage.com/300x400/00c853/ffffff&text=笔记本电脑',
      'qrCode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/laptop',
      'domain' => 'https://example.com'
    ]
  ];
  
  foreach ($products as $index => $productData) {
    $result = $htmlToImage->generateFromTemplate(
      'templates/product-poster.html',
      "product_batch_{$index}",
      $productData,
      true,
      $defaultOptions
    );
    
    if ($result['success']) {
      echo "商品 '{$productData['title']}' 海报生成成功！\n";
    } else {
      echo "商品 '{$productData['title']}' 海报生成失败: " . $result['error'] . "\n";
    }
  }
}

// HTML字符串示例
function runStringExample($htmlToImage, $defaultOptions) {
  echo "\n=== 从HTML字符串生成海报示例 ===\n";
  
  $templateContent = file_get_contents(__DIR__ . '/../templates/basic-poster.html');
  if ($templateContent === false) {
    echo "无法读取模板文件内容\n";
    return;
  }
  
  $customData = [
    'title' => '定制HTML生成的海报',
    'subtitle' => '通过字符串方式',
    'text' => '这是通过HTML字符串直接生成的海报示例，可以灵活定制各种内容和样式。',
    'price' => '¥88.88',
    'isShowPrice' => true,
    'isShowShopInfo' => true,
    'mainImage' => 'https://dummyimage.com/400x300/009688/ffffff&text=自定义海报',
    'qrcode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/custom',
    'shopLogo' => 'https://dummyimage.com/200x80/f44336/ffffff&text=自定义店铺',
    'shopName' => '自定义店铺名称',
    'shopDesc' => '自定义店铺描述',
    'domain' => 'https://example.com'
  ];
  
  $htmlString = preg_replace(
    [
      '/<h1 class="poster-title"[^>]*>(.*?)<\/h1>/',
      '/<h2 class="poster-subtitle"[^>]*>(.*?)<\/h2>/',
      '/<p class="poster-text"[^>]*>(.*?)<\/p>/',
      '/id="price-tag">([^<]+)</'
    ],
    [
      '<h1 class="poster-title" id="poster-title">' . $customData['title'] . '</h1>',
      '<h2 class="poster-subtitle" id="poster-subtitle">' . $customData['subtitle'] . '</h2>',
      '<p class="poster-text" id="poster-text">' . $customData['text'] . '</p>',
      'id="price-tag">' . $customData['price'] . '<'
    ],
    $templateContent
  );
  
  $result = $htmlToImage->generateFromHtmlString(
    $htmlString,
    'string_poster_example',
    true,
    $defaultOptions
  );
  
  if ($result['success']) {
    echo "HTML字符串海报生成成功！\n";
    echo "文件路径：\n";
    foreach ($result['files'] as $type => $path) {
      if ($path) echo "- {$type}: {$path}\n";
    }
  } else {
    echo "生成失败: " . $result['error'] . "\n";
  }
}

// 会员海报示例
function runMemberExample($htmlToImage, $defaultOptions) {
  echo "\n=== 会员邀请海报模板示例 ===\n";
  
  // 开启调试模式
  $htmlToImage->setDebug(true);
  
  $htmlToImage->setDefaultDomain('https://example.com');
  echo "已设置默认域名: " . $htmlToImage->getDefaultDomain() . "\n";
  
  $memberPosterData = [
    'userName' => '小皮',
    'shopName' => 'php智能店铺',
    'avatar' => 'https://robohash.org/phper?set=set4&size=150x150',
    'storeLogo' => 'https://dummyimage.com/200x80/4a86e8/ffffff&text=php智能店铺',
    'qrCode' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=https://example.com/member',
    'template' => 1,
    'background' => '',
    'text' => '邀请您加入我们的会员俱乐部，尊享VIP专属折扣与服务！',
    'isShopInfo' => true,
    'isStoreLogo' => true
  ];
  
  echo "生成海报使用的数据: " . json_encode($memberPosterData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
  
  $result = $htmlToImage->generateFromTemplate(
    'templates/member-poster.html',
    'member_poster_example',
    $memberPosterData,
    true,
    $defaultOptions
  );
  
  if ($result['success']) {
    echo "会员邀请海报生成成功！\n";
    echo "文件路径：\n";
    foreach ($result['files'] as $type => $path) {
      if ($path) echo "- {$type}: {$path}\n";
    }
  } else {
    echo "生成失败: " . ($result['error'] ?? '未知错误') . "\n";
  }
}

// 根据命令行参数运行示例
switch ($example_type) {
  case 'basic':
    runBasicExample($htmlToImage, $defaultOptions);
    break;
  case 'product':
    runProductExample($htmlToImage, $defaultOptions);
    break;
  case 'event':
    runEventExample($htmlToImage, $defaultOptions);
    break;
  case 'batch':
    runBatchExample($htmlToImage, $defaultOptions);
    break;
  case 'string':
    runStringExample($htmlToImage, $defaultOptions);
    break;
  case 'member':
    runMemberExample($htmlToImage, $defaultOptions);
    break;
  case 'all':
    runBasicExample($htmlToImage, $defaultOptions);
    runProductExample($htmlToImage, $defaultOptions);
    runEventExample($htmlToImage, $defaultOptions);
    runBatchExample($htmlToImage, $defaultOptions);
    runStringExample($htmlToImage, $defaultOptions);
    runMemberExample($htmlToImage, $defaultOptions);
    break;
  default:
    echo "未知的示例类型: {$example_type}\n";
    echo "可用的示例类型: basic, product, event, batch, string, member, all\n";
    exit(1);
}

echo "\n海报生成完成！\n";